package swingdisplay;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;

import shortcuthelper.ShortcutHelper;
import entry.Entry;

public class FrameDisplay extends JFrame implements ActionListener {
	private static final long serialVersionUID = 1L;
	private JTextField txtAbbr;
	private JTextField txtExtension;
	private JTextArea txtDescription;
	JButton btnAddToList = new JButton("Add");
	JButton btnEditOnList = new JButton("Edit");
	JButton btnRemove = new JButton("Remove");
	JButton btnImport = new JButton("IMPORT");
	JButton btnExport = new JButton("EXPORT");
	File importFile;
	File exportFile;
	ShortcutHelper sh;
	EntriesTableModel model;
	private JTable table;

	public FrameDisplay(ShortcutHelper sh) {
		super("Shortcut Helper");
		this.sh = sh;
		setWindow();
		setTxtAbbr();
		setTxtExtension();
		setTxtDescription();
		setButtons();
		setTable();
		setListeners();
		setScrollPane();
		setVisible(true);
	}

	private void setScrollPane() {
		JScrollPane scrollPane = new JScrollPane(table);
		scrollPane.setBounds(298, 11, 176, 432);
		getContentPane().add(scrollPane);
	}

	private void setTable() {
		model = new EntriesTableModel();
		model.setList(sh.getList());
		table = new JTable(model);
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
	}

	private void setWindow() {
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(500, 500);
		setLocation(50, 50);
		getContentPane().setLayout(null);
	}

	private void setButtons() {
		btnAddToList.setBounds(7, 386, 89, 23);
		getContentPane().add(btnAddToList);
		btnEditOnList.setBounds(106, 386, 89, 23);
		getContentPane().add(btnEditOnList);
		btnRemove.setBounds(205, 386, 89, 23);
		getContentPane().add(btnRemove);
		btnImport.setBounds(59, 420, 89, 23);
		getContentPane().add(btnImport);
		btnExport.setBounds(158, 420, 89, 23);
		getContentPane().add(btnExport);
	}

	private void setTxtDescription() {
		txtDescription = new JTextArea();
		txtDescription.setText("Description");
		txtDescription.setBounds(10, 120, 251, 255);
		getContentPane().add(txtDescription);
	}

	private void setTxtExtension() {
		txtExtension = new JTextField();
		txtExtension.setText("Extension");
		txtExtension.setBounds(10, 59, 251, 20);
		getContentPane().add(txtExtension);
		txtExtension.setColumns(10);
	}

	private void setTxtAbbr() {
		txtAbbr = new JTextField();
		txtAbbr.setText("Shortcut");
		txtAbbr.setBounds(10, 11, 86, 20);
		getContentPane().add(txtAbbr);
		txtAbbr.setColumns(10);
	}

	private void setListeners() {
		btnAddToList.addActionListener(this);
		btnEditOnList.addActionListener(this);
		btnRemove.addActionListener(this);
		btnImport.addActionListener(this);
		btnExport.addActionListener(this);
		table.addMouseListener(new MouseAdapter() {
			
			@Override
			public void mouseClicked(MouseEvent arg0) {
				int selectedRow = table.getSelectedRow();
				setEntry(sh.getList().get(selectedRow));
			}
		});
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btnAddToList) {
			addEntry();
		}
		if (e.getSource() == btnEditOnList) {
			editEntry();
		}
		if (e.getSource() == btnRemove) {
			removeEntry();
		}

		if (e.getSource() == btnImport) {
			JFileChooser chooser = new JFileChooser();
			int returnVal = chooser.showOpenDialog(this);
			if (returnVal == JFileChooser.APPROVE_OPTION) {
				sh.importList(chooser.getSelectedFile());
			model.setList(sh.getList());
			model.fireTableDataChanged();
			}
			System.out.println(sh.getList().size());
		}

		if (e.getSource() == btnExport) {
			JFileChooser chooser = new JFileChooser();
			int returnVal = chooser.showOpenDialog(this);
			if (returnVal == JFileChooser.APPROVE_OPTION)
				sh.exportList(chooser.getSelectedFile());
		}

	}

	public void addEntry() {
		sh.addToList(getEntry());
		model.fireTableDataChanged();
	}

	public void removeEntry() {
		int selectedRow = table.getSelectedRow();
		sh.removeFromList(sh.getList().get(selectedRow));
		model.fireTableDataChanged();
	}

	public void editEntry() {
		int selectedRow = table.getSelectedRow();
		sh.addToList(selectedRow, getEntry());
		model.fireTableDataChanged();
	}
	

	public Entry getEntry() {
		Entry entry = new Entry();
		entry.setAbbr(txtAbbr.getText());
		entry.setDescription(txtDescription.getText());
		entry.setExtension(txtExtension.getText());
		return entry;
	}
	
	public void setEntry(Entry entry) {
		txtAbbr.setText(entry.getAbbr());
		txtDescription.setText(entry.getDescription());
		txtExtension.setText(entry.getExtension());
	}

}
